<?php
/**
 * Created by PhpStorm.
 * User: 96151
 * Date: 2019/3/4
 * Time: 20:59
 */
namespace app\common\service\swooleJobs;

use app\common\model\AuthorModel;
use app\common\model\BookModel;
use app\common\model\ChapterModel;
use app\common\service\ElkLog;
use app\common\service\spider\QuShu;
use app\common\service\SwooleJob;
use app\common\service\SwooleJob\WeChatPushBook;


class BookJobs
{
    /**
     * 初始化趣书一篇小说
     */
    public static function QuShuFormatJobs( $url )
    {
        SwooleJob::push( SwooleJob::$MY_JOBS ,__CLASS__,"QuShuFormat" , array( $url ) );
    }

    /**
     * 获取小说数据
     */
    public static function QuShuSelectBookJobs( $id, $url )
    {
        SwooleJob::push( SwooleJob::$MY_JOBS ,__CLASS__,"QuShuSelectBook" , array( $id, $url ) );
    }

    /**
     * 检测一篇小数是否有更新
     * @param $url
     * @return bool
     */
    public function QuShuSelectBook( $id, $url )
    {
        $QuShu = new QuShu();
        $book_info = $QuShu->get_content( $url );

        if ( !$book_info ){
            return false;
        }

        // 获取文章最后一篇更新的数据
        $Chapter = new ChapterModel();

        $chapter_info = $Chapter->get_book_last_update_chapter( $id );

        if ( count( $book_info['list'] ) < $chapter_info['sort'] ){
            return false;
        }

        $list = array_slice( $book_info['list'], $chapter_info['sort'] + 1);
        $sort = $chapter_info['sort'] + 1;

        foreach ( $list as $value ){
            $add = array(
                "book_id" => $id,
                "sort" => $sort,
                "title" => $value['title'],
                "url"   => $value['url']
            );

            $sort++;
            $chapter_id = $Chapter->insertGetId( $add );

            // 加入推送队列
            WeChatPushBook::BookUpdatePushJobs( $chapter_id );
        }

        $Book = new BookModel();

        $update = array(
            "last_update_time" => $book_info['last_update_time']
        );

        $Book->save( $update ,[ "id" => $id ] );

        return true;
    }


    /**
     * 初始化趣书一篇小说
     * @param $url
     */
    public function QuShuFormat( $url )
    {
        ElkLog::write( array( "message" => $url ) );

        $QuShu = new QuShu();
        $book_info = $QuShu->get_content( $url );

        if ( !$book_info ){
            return false;
        }

        ElkLog::write( array( "message" => "{$book_info['title']}开始初始化" ) );

        // 作者处理
        $Author = new AuthorModel();
        $author_data = $Author->get_or_add_info( $book_info['author'] );
        $book_info['author_id'] = $author_data['id'];

        $list = $book_info['list'];
        unset( $book_info['list']);

        // 书处理
        $Book = new BookModel();

        $book_id = $Book->insertGetId( $book_info );

        $add_list = array();
        foreach ( $list as $key => $value ){
            $add_list[] = array(
                "book_id" => $book_id,
                "sort" => $key,
                "title" => $value['title'],
                "url"   => $value['url']
            );
        }

        $Chapter = new ChapterModel();
        $add_list_list = array_chunk( $add_list, 50 );
        foreach ( $add_list_list as $list ){
            $Chapter->insertAll( $list );
        }

        return true;
    }



}